package
        com.pinyoumei.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.aliyun.player.AliPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.source.UrlSource;
import com.pinyoumei.app.R;
import com.pinyoumei.app.utils.LogUtils;
import com.pinyoumei.app.view.LoadingDailog;



/**
 * 视频弹框
 */
public class VideoPlayDialog extends Dialog {
    private Context context;
    private String url;
    private AliPlayer aliyunVodPlayer;
    private LoadingDailog loadingDailog;

    public VideoPlayDialog(Context context, String url){
        super(context);
        this.context = context;
        this.url = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoadingDailog.Builder builder = new LoadingDailog.Builder(context)
                .setMessage("加载中...")
                .setCancelable(true);
        loadingDailog = builder.create();
        loadingDailog.show();
        Window window = getWindow();//获取一个窗口
        window.setGravity(Gravity.BOTTOM);//设置窗口的位置
        window.setWindowAnimations(R.style.bottom_menu_animation);//设置窗口进入时的动画特效
        WindowManager.LayoutParams layoutParams = window.getAttributes();//获取窗口的属性赋值给layoutParams
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);//设置窗口的属性
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.item_video_play);


       SurfaceView surfaceView_first = findViewById(R.id.surfaceView_first);
       RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               dismiss();
            }
        });


        aliyunVodPlayer = AliPlayerFactory.createAliPlayer(context);
        UrlSource urlSource = new UrlSource();
        urlSource.setUri(url);
        //设置播放源
        aliyunVodPlayer.setDataSource(urlSource);
        //准备播放
        aliyunVodPlayer.prepare();

        aliyunVodPlayer.isAutoPlay();
        aliyunVodPlayer.setLoop(true);

        surfaceView_first.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                aliyunVodPlayer.setDisplay(holder);

                loadingDailog.dismiss();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                aliyunVodPlayer.redraw();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                aliyunVodPlayer.setDisplay(null);
                aliyunVodPlayer.stop();
            }
        });

        aliyunVodPlayer.start();
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity=Gravity.BOTTOM;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

    @Override
    public void setOnCancelListener(@Nullable OnCancelListener listener) {
        super.setOnCancelListener(listener);
        aliyunVodPlayer.stop();

        LogUtils.e("video暂停");
    }
}
